<template>
  <div class="header-pc-wrap">

    <Header></Header>
    <div style="background: #eee;">
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right" style="width:200px">
        <el-breadcrumb-item :to="{ path: '/' }">课程列表</el-breadcrumb-item>
        <el-breadcrumb-item>账号设置</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- tab 标签页 -->
    <div>
      <el-radio-group v-model=tabPosition style="margin-bottom: 30px;">
      </el-radio-group>

      <el-tabs :tab-position="tabPosition" style="height: 200px;">
        <el-tab-pane label="基本设置">
           <div style="width:200px">
             上传头像
             <!-- <el-upload
              class="upload-demo"
              action="uploadImgUrl"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :before-remove="beforeRemove"
              multiple
              :limit="3"
              :on-exceed="handleExceed"
              :file-list="fileList">
              <el-button size="small" type="primary" @click="upload">点击上传</el-button>
            </el-upload> -->
            <input type="file" ref="myfile" name="上传头像">
           </div>
           <p></p>
           <div style="width:200px">
             <div>修改昵称</div>
             <el-input v-model="nickName" placeholder="请输入昵称" style="width:200px"></el-input>
           </div>
            <p></p>
           <el-button type="success" @click="update">更新信息</el-button>
        </el-tab-pane>
        <el-tab-pane label="修改密码" >
          <div style="width:200px" @click="goToModifyPassword">
              账户密码
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 密码修改框 开始-->
    <el-dialog
      style="width:1000px;margin:0px auto;"
      title=""
      :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item>
          <h1 style="font-size:20px;color:#00B38A">修改密码</h1>
        </el-form-item>
        <el-form-item>
          当前密码
          <el-input v-model="oldPassword" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item>
          新密码
          <el-input v-model="newPassword" style="width:300px"></el-input>
        </el-form-item>
        <el-form-item>
          确认密码
          <el-input v-model="newPassword1" style="width:300px"></el-input>
        </el-form-item>
      </el-form>
      <el-button
        style="width:20%;margin:0px auto;background-color: #00B38A;font-size:20px"
        type="primary"
        @click="updatePassword">确 定
      </el-button>
      <el-button type="text" @click="quxiao">取消</el-button>
      
    </el-dialog>
    <!-- 框 结束-->
     
  </div>

</template>

<script>
import Header from "./Header/Header"; //顶部登录条
export default {
  name: "setting",
  components:{
   Header
  },
  props: {},
  data() {
    return {
      tabPosition: 'left',
      isLogin: false, // 登录状态，true：已登录，false：未登录
      userDTO:null, // 用来保存登录的用户信息
      isHasNewMessage: false, // 是否有新的推送消息
      dialogFormVisible: false, // 是否显示框，true：显示，false：隐藏
      // imageUrl:"",
      nickName:"",
      oldPassword: "", // 双向绑定表单
      newPassword: "", // 双向绑定表单
      newPassword1: "", // 双向绑定表单
      fileList:[],
    };
  },
  computed: {
  },
  watch: {
  },
  mounted() {
  },
  created(){
    //当刷新页面，组件创建成功后，立刻检测本地储存中是否存在用户对象
    this.userDTO = JSON.parse(localStorage.getItem("user"));
    if (this.userDTO != null){
      this.isLogin = true;//已登录
    }
  },
  methods: {
    update(){
      console.log(this.userDTO);
      var formdata = new FormData();//定义传递的参数
      //放置用户id
      formdata.append("userid", this.userDTO.content.id);
      this.fileList = this.$refs.myfile.files;
      console.log(this.fileList);
      if (this.fileList.length > 0){
        //放置图片
        formdata.append("file", this.fileList[0], this.fileList[0].name);
      }
      //放置昵称
      formdata.append("nickname", this.nickName);
      //发送请求
      const config = {
        headers: { "enctype": "multipart/form-data"}
      };
      return this.axios
      .post("http://localhost:8002/user/updateUserInfo/",formdata,config)
      .then((result) =>{
          console.log(result);
          if(result.data.state == 200){
            this.$message.success("更新成功！");
            this.userDTO.content.name = this.nickName;
            this.userDTO.content.portrait = result.data.content.portrait;
          }
        }).catch((error)=>{
          this.$message.error("更新信息失败！");
      })
    },
    goToModifyPassword(){
      //打开对话框
      this.dialogFormVisible = true;
      console.log( this.dialogFormVisible);
    },
    updatePassword(){
      //判断原始密码是否正确
      if (this.userDTO.content.password != this.oldPassword){
        this.$message.error("原始密码错误！");
        this.dialogFormVisible = false;
        return;
      }
      //判断两次新密码是否一样
      if (this.newPassword != this.newPassword1){
        this.$message.error("新密码两次不一致！");
        this.dialogFormVisible = false;
        return;
      }
      //此时调用后端方法
      return this.axios
      .get("http://localhost:8002/user/updatePassword/?"+"userid="+this.userDTO.content.id +"&password="+this.newPassword)
      .then((result) =>{
          console.log(result);
          if(result.data.state == 200){
            this.$message.success("更新成功！");
            //此时需要重新加载用户信息(重新赋值密码)
            this.userDTO.content.password = this.newPassword;
          }
          this.dialogFormVisible = false;
        }).catch((error)=>{
          this.$message.error("更新信息失败！");
          this.dialogFormVisible = false;
      })
    },
    quxiao(){
       this.dialogFormVisible = false;
    }
  },
};
</script>

<style lang="less" scoped>
.header-pc-wrap {
  width: 100%;
  height: 40px;
  background: rgba(35, 39, 43, 1);
}
.wrap-box {
  width: 1200px;
  height: 100%;
  margin: 0 auto;
}
.edu-icon {
  float: left;
  width: 24px;
  height: 24px;
  background: url("./Header/static/img/Icon@2x.png") no-repeat;
  background-size: 100% 100%;
  margin-top: 8px;
}
.text {
  font-size: 16px;
  font-weight: 500;
  color: rgba(255, 255, 255, 1);
  line-height: 40px;
  float: left;
  margin-left: 6px;
}
.login-handler {
  font-size: 12px;
  font-weight: 500;
  color: rgba(255, 255, 255, 1);
  line-height: 40px;
  float: left;
  cursor: pointer;
}
.right-var-wrap {
  float: right;
  font-size: 0;
  text-align: right;
}
.tip-icon,
.avatar-wrap,
.bar-wrap {
  display: inline-block;
  vertical-align: top;
}
.tip-icon {
  font-size: 16px;
  line-height: 40px;
  margin-right: 26px;
  color: #818895;
  cursor: pointer;
  &:hover {
    color: #fff;
  }
  &.has-new-message {
    position: relative;
    &:after {
      content: " ";
      display: inline-block;
      position: absolute;
      top: 50%;
      width: 6px;
      height: 6px;
      border-radius: 3px;
      background: red;
      right: 0;
      margin-top: -7px;
    }
  }
}
.user_dropdown {

  position: relative;
  cursor: pointer;
  font-size: 14px;
  text-align: center;
  &:hover {
    .unick {
      color: #fff;
    }
    i {
      -webkit-transform: rotate(180deg);
      -moz-transform: rotate(180deg);
      -ms-transform: rotate(180deg);
      -o-transform: rotate(180deg);
      transform: rotate(180deg);
      animation-fill-mode: forwards;
      border-color: #fff transparent transparent;
    }
    > ul {
      display: block;
      position: absolute;
      top: 40px;
      width: 100%;
      min-width: 80px;
      line-height: 30px;
      background-color: #32373e;
      z-index: 1000;
      list-style: none; margin:0px;padding:0px;
      > li {
        width: 100%;
        height: 30px;
        &:hover {
          background-color: #25282d;
          color: #fff;
        }
      }
    }
  }
  .unick {
    display: block;
    height: 40px;
    line-height: 40px;
    font-size: 14px;
    color: #afb5c0;
    max-width: 96px;
    margin: 0 9px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    word-wrap: normal;
    &:hover {
      color: #fff;
    }
  }
  > i {
    position: absolute;
    top: 17px;
    right: 0;
    font-size: 0;
    height: 0;
    width: 0;
    border-width: 5px 4px 0;
    border-style: solid dashed;
    border-color: #afb5c0 transparent transparent;
    overflow: hidden;
    -webkit-transition: all 0.4s ease 0s;
    -moz-transition: all 0.4s ease 0s;
    -ms-transition: all 0.4s ease 0s;
    -o-transition: all 0.4s ease 0s;
    transition: all 0.4s ease 0s;
  }
  > ul {
    display: none;
    color: #afb5c0;
  }
}
.avatar-wrap {
  width: 24px;
  height: 24px;
  margin-top: 8px;
  border-radius: 50%;
}
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>


